/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Assignment2.Q3;

/**
 *
 * @author SYS
 */
import java.net.*;
import java.io.*;

public class Echoer implements Runnable {

    private Socket s;
    private InputStream in;
    private OutputStream out;

    private boolean localEcho;

    public Echoer(Socket s) throws IOException {
        this(s.getInputStream(), s.getOutputStream(), true);
        this.s = s;
    }

    public Echoer(InputStream in, OutputStream out, boolean echo) {
        this.in = in;
        this.out  = out;
        this.localEcho = echo;
    }

    public void serviceConnection() throws IOException {
        int b = in.read();
        while (b != -1) {
            if (localEcho) {
                System.out.print((char)b);
            }
            out.write(b);
            b = in.read();
        }
    }

    public void run() {
        try {
            serviceConnection();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

